package com.css.fxfzysx.modules.vaolEngineering.repository.impl;

import com.css.fxfzysx.modules.lifeline.entity.newEntity.SmxRoadEntity;
import com.css.fxfzysx.modules.vaolEngineering.entity.CbVulnerabilityEntity;
import com.css.fxfzysx.modules.vaolEngineering.entity.CrVulnerabilityEntity;
import com.css.fxfzysx.modules.vaolEngineering.entity.CrbsForceEntity;
import com.css.fxfzysx.modules.vaolEngineering.entity.CssDesolderingEntity;
import com.css.fxfzysx.modules.vaolEngineering.param.update.RoadParam;
import com.css.fxfzysx.modules.vaolEngineering.repository.CarVulnerabilityRepository;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.EmptyStackException;
import java.util.List;

@Repository
public class CarVulnerabilityRepositoryImpl implements CarVulnerabilityRepository {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public SmxRoadEntity getById(String id) {
        String sql = "select * from czt_smx_road where id = '"+id+"'";
        try {
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(SmxRoadEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public List<CrVulnerabilityEntity> getVulnerabilityById(String id) {
        String sql = "select * from czt_road_vulnerability where project_id='"+id+"' and del_flag='0' order by create_time asc";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(CrVulnerabilityEntity.class));
    }

    @Override
    public boolean saveRoad(SmxRoadEntity entity) {
        String sql = "insert into czt_smx_road values(:id,:roadId,:name,:grade,:intensity,:roadbedSoil,:siteSoil,:fortification,:foundationFail,:roadbedType,:roadbedHigh,:year,:capacity,:pavement,:length,:width,:note,:createUser,:createUserName,:createTime,:updateUser,:updateTime,:delFlag,:orgName,:destructiveState,:destructiveStateCorrect,:ubInd,:oi,:p02,:p24,:p46,:p68,:p810,:basicIntact,:slightDamage,:moderateDamage,:badlyDamaged,:destroy,:startLongitude,:startLatitude,:endLongitude,:endLatitude,ST_GeometryFromText(:geom,4490),:versionId,:tableId,:serialNumber,:disasterRate6State,:disasterRate6StateCorrect,:disasterRate8State,:disasterRate8StateCorrect,:disasterRate9State,:disasterRate9StateCorrect,:disasterRate10State,:disasterRate10StateCorrect,:extends1,:extends2,:extends3,:extends4,:extends5,:extends6,:extends7,:extends8,:extends9,:extends10)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(entity);
        return namedParameterJdbcTemplate.update(sql, source) > 0;
    }

    @Override
    public List<SmxRoadEntity> getByVersionId(String id) {
        try {
            String sql = "select * from czt_smx_road where version_id = '"+id+"'";
            return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(SmxRoadEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public void saveRoadResult(SmxRoadEntity entity) {
        jdbcTemplate.update("update czt_smx_road set ub_ind=?,oi=?,p02=?,p24=?,p46=?,p68=?,p810=?,basic_intact=?,slight_damage=?,moderate_damage=?,badly_damaged=?,destroy=?,destructive_state=?,destructive_state_correct=?,disaster_rate6_state=?,disaster_rate7_state=?,disaster_rate8_state=?,disaster_rate9_state=?,disaster_rate10_state=?,disaster_rate6_state_correct=?,disaster_rate7_state_correct=?,disaster_rate8_state_correct=?,disaster_rate9_state_correct=?,disaster_rate10_state_correct=? where id = ?",
                entity.getUbInd(),entity.getOi(),entity.getP02(),entity.getP24(),entity.getP46(),entity.getP68(),entity.getP810(),entity.getBasicIntact(),entity.getSlightDamage(),entity.getModerateDamage(),entity.getBadlyDamaged(),entity.getDestroy(),
               entity.getDestructiveState(),entity.getDestructiveStateCorrect(),entity.getDisasterRate6State(),entity.getDisasterRate7State(),entity.getDisasterRate8State(),entity.getDisasterRate9State(),entity.getDisasterRate10State(),entity.getDisasterRate6StateCorrect(),entity.getDisasterRate7StateCorrect(),entity.getDisasterRate8StateCorrect(),entity.getDisasterRate9StateCorrect(),entity.getDisasterRate10StateCorrect(),entity.getId());
    }

    @Override
    public SmxRoadEntity getByTableId(String id) {
        String sql = "select * from czt_smx_road where table_id = '"+id+"'";
        try {
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(SmxRoadEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public void updateRoadById(RoadParam entity) {
        jdbcTemplate.update("update czt_smx_road set intensity=?,roadbed_soil=?,site_soil=?,foundation_fail=?,roadbed_type=?,roadbed_high=?,destructive_state_correct=?,fortification=?,ub_ind=?,oi=?,p02=?,p24=?,p46=?,p68=?,p810=?,basic_intact=?,slight_damage=?,moderate_damage=?,badly_damaged=?,destroy=?,destructive_state=?,rd_ids=?,dd_ids=? where id=?",
                entity.getIntensity(),entity.getRoadbedSoil(),entity.getSiteSoil(),entity.getFoundationFail(),entity.getRoadbedType(),entity.getRoadbedHigh(),entity.getDestructiveStateCorrect(),entity.getFortification(),entity.getUbInd(),entity.getOi(),entity.getP02(),entity.getP24(),entity.getP46(),entity.getP68(),entity.getP810(),entity.getBasicIntact(),entity.getSlightDamage(),entity.getModerateDamage(),entity.getBadlyDamaged(),entity.getDestroy(),entity.getDestructiveState(),entity.getRdIds(),entity.getDdIds(),entity.getId());
    }
}
